#include <stdio.h>
#include <ctype.h>
#include <malloc.h>
#include <assert.h>

//节点信息
typedef struct Node
{
    int data;
    struct Node *next;
}Node,*pNode;

pNode initList();
void addNodeInHead(pNode head);
void addNodeInTail(pNode head);
int listLen(pNode head);
void traverList(pNode head);
void insert(pNode head);
void deleteNode(pNode head);
void reverseList(pNode head);
void detoryList(pNode head);
//init list
pNode initList()
{
    pNode head = (pNode)malloc(sizeof(Node));
    if(NULL == head)
    {
        printf("Init List Failed!\n");
        return NULL;
    }
    head->next = NULL;

    return head;
}

//intsert in head
void addNodeInHead(pNode head)
{
    int curdata;
    printf("Intput Your Node Value:");
    scanf("%d",&curdata);
    while(1)
    {
        pNode curNode = (pNode)malloc(sizeof(Node));
        curNode->data = curdata;
        curNode->next = head->next;
        head->next = curNode;
        scanf("%d",&curdata);
        if(curdata == 0)
        {
            break;
        }
    }
}
//insert in tail
void addNodeInTail(pNode head)
{
    assert(head);
    int curdata;
    printf("Input Your Node Value:");
    scanf("%d",&curdata);
    pNode ptail = head;

    while(1)
    {
        pNode curNode = (pNode)malloc(sizeof(Node));
        curNode->data = curdata;
        curNode->next = NULL;
        ptail->next = curNode;
        ptail = curNode;
        scanf("%d",&curdata);
        if(curdata == 0)
        {
            break;
        }
    }
}

//list len
int listLen(pNode head)
{
    assert(head);
    int len  = 0;
    pNode p = head->next;
    while(p)
    {
        len++;
        p = p->next;
    }
    return len;
}
//traverslist
void traverList(pNode head)
{
    assert(head);
    pNode p = head->next;
    printf("TraverList is:");
    while(p != NULL)
    {
        printf("%d-->",p->data);
        p = p->next;
    }
    printf("\n");

}
void insert(pNode head)
{
    int pos;
    printf("Input your insert pos:");
    scanf("%d",&pos);

    int len = listLen(head);
    if(pos > len)
    {
        pos = len;//若大于链表长度则插入到最后的位置
    }
    pNode p = head;
    pNode curData = (pNode)malloc(sizeof(Node));
    int value;
    printf("Input Node Value:");
    scanf("%d",&value);
    curData->data = value;
    while(pos)
    {
        p = p->next;
        pos--;
    }
    curData->next = p->next;
    p->next = curData;
}

void deleteNode(pNode head)
{
    assert(head);
    printf("Input a pos numbwe(number>0):");
    int pos = 0;
    scanf("%d",&pos);
    pNode pre = head;
    pNode p = head->next;
    while(pos)
    {
        pos--;
        pre = pre->next;
        p = p->next;
    }
    printf("The delete numbwe is :%d\n",p->data);
    pre->next = p->next;
    free(p);
}

void reverseList(pNode head)
{
    assert(head);
    pNode p = head->next;
    pNode cur;
    while(p->next != NULL)
    {
        cur = p->next;
        p->next = cur->next;
        cur->next = head->next;
        head->next = cur;
    }

}

void destoryList(pNode head)
{
    assert(head);
    pNode subHead = head;
    while(head)
    {
        head = head ->next;
        free(subHead);
        subHead = NULL;
        subHead = head;
    }
}
int main()
{
    pNode head = initList();
//    addNodeInHead(head);
    addNodeInTail(head);
    traverList(head);
    int len = listLen(head);
    printf("list len is:%d\n",len);
    insert(head);
    traverList(head);
    //deleteNode(head);
    traverList(head);
    reverseList(head);
    traverList(head);
    destoryList(head);
    traverList(head);
    return 0;
}
